<?php
namespace Admin\Controller;

class MemberInviteController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $keyword = I('get.keyword');
            $where = array();
            if ($keyword) {
                $where['m.nickname|m.openid|im.nickname|im.openid'] = array( 'like', "%{$keyword}%" );
            }

            $model = M()->table('__MEMBER_INVITE__ AS i')->join('LEFT JOIN __MEMBER__ AS m ON i.member_id = m.member_id')->join('LEFT JOIN __MEMBER__ AS im ON im.member_id = i.invite_member_id');
            $field = 'i.invite_id,i.member_id,i.invite_member_id,i.coupon_id,i.is_subscribe,i.is_order,i.create_time,m.nickname,m.openid,im.nickname AS invite_nickname,im.openid AS invite_openid';
            $list = $this->lists($model, $field, $where, 'i.invite_id DESC', 10);
            $coupon = getCouponList(false);
            $this->assign('coupon', $coupon);
            $this->assign('list', $list);
            $this->assign('keyword', $keyword);
            $this->display();
        }
    }


    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'invite_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('MemberInvite')->where($where)->delete();
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }  
    
}